//手机号码正则表达式
var phoneReg = /^1[345789][0-9]{9}$/;
//验证码正则表达式
var codeReg =/^[0-9]{6}$/;
var pwdReg0 =/^.{6,16}$/;
var pwdReg1 =/[0-9]+/;
var pwdReg2 =/[a-zA-Z]+/;
var pwdReg3 = /[=~!@#\$%\^&\*\(\)_\+\{\[\}]:"\|',<?.\/\\]+/;
var timer;
var t =60;
//表示是否同意注册协议
var isAgree = false;
var isPhoneRight = false;
var isYZMRight = false;
var isPwd1Right = false;
var isPwd2Right = false;
//功能1：向用户提示手机号码格式是否正确，决定是否用验证码按钮
phone.onblur = function(){
    phoneValue =phone.value;
    if(!phoneReg.test(phoneValue)){
        phoneerror.innerHTML = "手机号码格式有误！";
        isPhoneRight = false;
    }else{
        phoneerror.innerHTML = "<img src='dui.png' />";
        isPhoneRight = true;
    }
    checkIsHavePhone();
    checkAll();
};
function checkIsHavePhone(){
    $.get("http://test.hfjava.com/shop/userphone?phone="+phone.value,{},function(data){
        if(data=="false"){
            phoneerror.innerHTML = "此号码已经注册！";
            isPhoneRight = false;
        }
    });
}

//功能2：根据手机号码格式的正确性，决定是否启用验证码按钮
phone.onkeyup = function (){
    var phoneValue = phone.value;
    if(!phoneReg.test(phoneValue)){
        yanzhengmaBtn.setAttribute("disabled","disabled");
        yanzhengmaBtn.style.color = "";
        isPhoneRight = false;
    }else{
        yanzhengmaBtn.removeAttribute("disabled");
        yanzhengmaBtn.style.color = "red";
        isPhoneRight = true;
    }
    checkAll();
};
//功能3：发送验证码功能
//点击后要求倒计时60秒，在此期间该按钮应该禁用
yanzhengmaBtn.onclick = function(){
    yanzhengmaBtn.setAttribute("disabled","disabled");
    yanzhengmaBtn.style.color = "";
    yanzhengmaBtn.value = "重新发送60";

    timer = setInterval(daojishi,1000);
};
function daojishi(){
    t--;
    if(t==0){
        clearInterval(timer);
        yanzhengmaBtn.value = "发送验证码";
        yanzhengmaBtn.removeAttribute("disabled");
        yanzhengmaBtn.style.color = "red";
        t = 60;
        return;
    }
    yanzhengmaBtn.value = "重新发送"+t;
}

//功能4：验证验证码正确性
yzm.onkeyup = function(){
    var yzmValue = yzm.value;
    if(yzmValue.length ==0){
        yzmerror.innerHTML = "请重新填写验证码！";
        isYZMRight = false;
        checkAll();
        return;
    }
    if(!codeReg.test(yzmValue)){
        yzmerror.innerHTML = "验证码格式错误！";
        isYZMRight = false;
        checkAll();
        return;
    }
$.ajax({
    url:"http://test.hfjava.com/shop/yzm",
    dataType:"jsonp",
    success:function(data){
        if(data.code!=yzmValue){
            yzmerror.innerHTML = "验证码错误！";
            isYZMRight = false;
        }else{
            yzmerror.innerHTML = "验证码正确！";
            isYZMRight = true;
        }
        checkAll();
      }
  });
};

//功能5：密码1验证格式有效性
pwd1.onkeyup = function() {
    var pwd1Value = pwd1.value;
    var rightCount = 0;
    //长度 字母 数字 符号
    var lengthValid = pwdReg0.test(pwd1Value);
    var numberValid = pwdReg1.test(pwd1Value);
    var letterValid = pwdReg2.test(pwd1Value);
    var symbolValid = pwdReg3.test(pwd1Value);

    if(numberValid){
        rightCount++;
    }
    if(letterValid){
        rightCount++;
    }
    if(symbolValid){
        rightCount++;
    }
    if(!lengthValid||rightCount<2){
        pwderror1.innerHTML = "密码格式错误！";
        isPwd1Right = false;
    }else{
        pwderror1.innerHTML = "密码格式正确";
        isPwd1Right = true;
    }
    checkPwd2();
    checkAll();

};

//功能6：密码2验证码与密码1是否一致
pwd2.onkeyup = function (){
    checkPwd2();
};
function checkPwd2(){
    var pwd1Value = pwd1.value;
    var pwd2Value = pwd2.value;
    if(pwd1Value!==pwd2Value){
        pwderror2.innerHTML = "两次输入密码不一致";
        isPwd2Right = false;
    }else{
        pwderror2.innerHTML = "两次输入密码一致";
        isPwd2Right = true;
    }
    checkAll();
}
//功能7：获得用户是否同意注册协议
agreeCK.onclick = function(){
    isAgree = this.checked;
    checkAll();
};
//功能8：检测是否可以注册
function checkAll(){
    var isRight = isAgree&&isPhoneRight&&isYZMRight&&isPwd1Right&&isPwd2Right;
    if(isRight){
        submitBtn.removeAttribute("disabled");
        submitBtn.style.color= "red";
    }else{
        submitBtn.setAttribute("disabled","disabled");
        submitBtn.style.color ="";
    }
}
//功能9：实现注册功能
submitBtn.onclick = function(){
    $.post("http://test.hfjava.com/shop/adduser",
        function (data){
            if(data=="Saved"){
                console.log("注册成功!");
            }
        });
};